﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
namespace HF.Architecture.Data
{
    /// <summary>
    /// 数据访问类DutyData。
    /// </summary>
    public class DutyData
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "DutyId", "FatherId", "Caption", "DutyLevel", "Description" };
        private string tableName = "HF_Duty";
        private string keyField = "DutyId";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(DutyEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@DutyId", entity.DutyId, typeof(string));
            sqlDataItem.AppendParameter("@FatherId", entity.FatherId, typeof(string));
            sqlDataItem.AppendParameter("@Caption", entity.Caption, typeof(string));
            sqlDataItem.AppendParameter("@DutyLevel", entity.DutyLevel, typeof(string));
            sqlDataItem.AppendParameter("@Description", entity.Description, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

        

        #endregion
        #region  构造函数
        public DutyData()
        { }
        public static DutyData GetInstance()
        {
            return new DutyData();
        }
        #endregion
        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(DutyEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(DutyEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string dutyId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, dutyId);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="dutyId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string dutyId)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, dutyId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="DutyId">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetDutyTable(string dutyId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, dutyId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="dr">数据表记录</param>
        /// <returns>数据实体</returns>
        public DutyEntity GetDutyEntity(DataRow dr)
        {
            DutyEntity entity = new DutyEntity();
            entity.DutyId = dr["DutyId"].ToString();
            entity.FatherId = dr["FatherId"].ToString();
            entity.Caption = dr["Caption"].ToString();
            entity.DutyLevel = dr["DutyLevel"].ToString();
            entity.Description = dr["Description"].ToString();
            return entity;
        }
        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="dutyId">主键Id</param>
        /// <returns>DataTable</returns>
        public DutyEntity GetDutyEntity(string dutyId)
        {
            DataTable dt = GetDutyTable(dutyId);
            if (dt != null && dt.Rows.Count > 0)
            {
                return GetDutyEntity(dt.Rows[0]);
            }
            return null;
        }

        #endregion
        #region  扩展方法
        /// <summary>
        /// 获得职务的所有子职务
        /// </summary>
        /// <param name="FatherClassId">父职务Id</param>
        /// <returns>子职务table</returns>
        public DataTable GetChildDutys(string dutyId)
        {
            try
            {
                string tmpStr = "select * from HF_Duty where  cast(FatherId  as Varchar(50) )=@FatherId";     //todo  zhuyun
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@FatherId", dutyId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得职务的所有子职务
        /// </summary>
        /// <param name="FatherClassId">父职务Id</param>
        /// <returns>子职务table</returns>
        public List<DutyEntity> GetChildDutyEntitys(string dutyId)
        {
            try
            {
                List<DutyEntity> entitys = new List<DutyEntity>();
                DataTable dt = GetChildDutys(dutyId);
                foreach (DataRow dr in dt.Rows)
                {
                    DutyEntity entity = GetDutyEntity(dr);
                    entitys.Add(entity);
                }
                return entitys;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #endregion
    }
}

